import { useMutation } from "@apollo/react-hooks";
import gql from "graphql-tag";
import { notification } from "antd";

import { QUERY } from "./useTable";
import { handleErrors } from "../../../utils/handleErrors";
import { SYS_AUTHORITIES } from "../../SysRole/hooks/useAuthorities";

const DELETE = gql`
  mutation DeleteSysAuthority($id: String!) {
    deleteSysAuthority(id: $id) {
      id
    }
  }
`;

export const useDelete = () => {
  const [deleteSysAuthority] = useMutation(DELETE, {
    onCompleted: ({ deleteSysAuthority }) => {
      if (deleteSysAuthority) {
        notification.success({
          message: `权限已删除！`
        });
      }
    },
    onError: ({ graphQLErrors, networkError }) => {
      handleErrors({ graphQLErrors, networkError });
    },
    refetchQueries: [
      {
        query: QUERY
      },
      {
        query: SYS_AUTHORITIES
      }
    ]
  });

  const handleDelete = id => {
    deleteSysAuthority({ variables: { id } });
  };

  return {
    handleDelete
  };
};
